package ac.you.api.ask.e;

import ac.you.api.ask.Ask;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;

import java.util.function.Consumer;

public abstract class Tasker<T> implements Listener, Mission<T> {
    public enum State{
        NOSTART(0),
        RUNNING(1),
        END(2);
        private final int i;
        State(int g){this.i = g;}
        public int g() {return i;}
    }
    private String name;
    private State state;
    private Ask ask;
    private ExCondition exCondition;
    private Consumer<Ask> after;
    private long outTime;
    private boolean isrunafter = false;
    private ResultHandler<T> handle;
    protected T lastValue;
    private volatile T value;

    @Override
    public void run() {}
    @Override
    public void accept(T t) {}
    @Override
    @Deprecated
    public T get() {
        while (value == null);
        return value;
    }

    protected void done(T t){
        value = t;
        handle.finish(t);
    }

    public final void ot(long sec){
        if (!ask.isRunning()){
            this.outTime = sec * 1000L;
        }
    }
    public final void na(String name){
        if (!ask.isRunning()){
            this.name = name;
        }
    }
    public final void ex(ExCondition e){
        if (!ask.isRunning()){
            this.exCondition = e;
        }
    }
    public final void af(Consumer<Ask> after){
        if (!ask.isRunning()){
            this.after = after;
        }
    }
    public final void h(ResultHandler<T> handle){
        if (!ask.isRunning()){
            this.handle = handle;
        }
    }
    public final void i(Ask a){
        if (this.ask == null){
            this.ask = a;
        }
    }
    public final void s(State state){
        if (!ask.isRunning() && (this.state == null || state.g() > this.state.g())){
            this.state = state;
        }
    }
    public final void lv(T v){
        this.lastValue = v;
    }
    public final void ra(boolean isrunafter) {
        this.isrunafter = isrunafter;
    }
    public Ask getAsk(){
        return ask;
    }

    public boolean hasEntityIn(LivingEntity entity){
        return ask != null && ask.hasEntity(entity);
    }

    public ExCondition getExCondition() {
        return exCondition;
    }

    public ResultHandler<T> getHandle() {
        return handle;
    }

    public State getState() {
        return state;
    }

    public long getOutTime() {
        return outTime;
    }

    public String getName() {
        return name;
    }

    public Consumer<Ask> getAfter() {
        return after;
    }

    public boolean isIsrunafter() {
        return isrunafter;
    }
    public T lv(){
        return lastValue;
    }

    @Override
    public String toString() {
        return "Tasker{" +
                "name='" + name + '\'' +
                '}';
    }
}
